import { BaseCommand, ICmdDialogParams } from '@amcax/base';
import i18n from 'i18next';
import { PickableCommandForm } from '@app-cad/common/components/command-form/pickable';
import { useState } from 'react';
import { RJSFSchema } from '@rjsf/utils';
import { wsApi } from '@app-cad/server';

const ImportDocument = ({ title, onClose }: ICmdDialogParams) => {
  const [formData, setFormData] = useState({ documentId: '', version: '1' });

  const schema: RJSFSchema = {
    type: 'object',
    properties: {
      documentId: {
        title: '文档ID',
        type: 'string',
      },
      version: {
        title: '版本',
        type: 'string',
      },
    },
  };

  return (
    <PickableCommandForm
      onClose={onClose}
      title={title}
      schema={schema}
      formData={formData}
      onChange={(v) => {
        setFormData(v);
      }}
      submitText='完成'
      onSubmit={() => wsApi.importDocument(formData)}
    />
  );
};

export const cmdImportDocument: BaseCommand = {
  id: 'CmdImportDocument',
  label: 'command.import-document',
  icon: 'commands/curved/import',
  noHighlight: true,

  create: (label, onClose) => (
    <ImportDocument
      title={i18n.t(label)}
      onClose={onClose}
    />
  ),
};
